package com.example.aqarapp.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.NumberPicker;

import androidx.annotation.NonNull;

import java.lang.reflect.Field;
import aqarapp.R;

public class MyNumberPicker extends NumberPicker {

    private int textX, textY;
    private String unitText;
    private Paint paint;

    public MyNumberPicker(Context context) {
        super(context);
    }

    public MyNumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (attrs == null) throw new RuntimeException("DateNumberPicker只能从xml布局文件调用");
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.UnitNumberPicker);
        unitText = ta.getString(R.styleable.UnitNumberPicker_unitText);
        float unitTextSize = ta.getDimension(R.styleable.UnitNumberPicker_unitTextSize, 80);
        int unitTextColor = ta.getColor(R.styleable.UnitNumberPicker_unitTextColor, Color.GRAY);
        ta.recycle();
        paint = new Paint();
        paint.setColor(unitTextColor);
        paint.setTextSize(unitTextSize);
        paint.setAntiAlias(true);
        post(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(unitText)) return;
                Rect rect = new Rect();
                paint.getTextBounds(unitText, 0, unitText.length(), rect);
                textX = getWidth() - rect.width() - 20;
                textY = getHeight() / 3 + rect.height() + 20;
            }
        });
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        if (!TextUtils.isEmpty(unitText)) {
            canvas.drawText(unitText, textX, textY, paint);
        }
    }


    @Override
    public void addView(View child) {
        super.addView(child);
        updateView(child);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        updateView(child);
    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        super.addView(child, params);
        updateView(child);
    }

    /**
     * 修改字的大小和颜色
     */
    private void updateView(View view){
//        if( view instanceof EditText){
//            EditText editText = (EditText) view;
//            editText.setTextColor(Color.parseColor("#00ff00")); //修改字的颜色
//            editText.setTextSize(30);//修改字的大小
//        }

//        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
//        for (Field pf : pickerFields){
//            if(pf.getName().equals("mInputText")){ //
//                pf.setAccessible(true);
//
//                //设置分割线的颜色
//                try {
//                    Log.d("yejianfei", "updateView: this = " + pf.get(this));
////                    ((EditText) pf.get(this)).setTextSize(30);
//                    ((EditText) pf.get(this)).setTextColor(Color.parseColor("#00ff00"));
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
    }

//    @Override
//    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
//        super.onLayout(changed, left, top, right, bottom);
//        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
//        for (Field pf : pickerFields){
//            if(pf.getName().equals("mInputText")){ //
//                pf.setAccessible(true);
//
//                //设置分割线的颜色
//                try {
//                    ((EditText) pf.get(this)).setTextSize(30);
//                    ((EditText) pf.get(this)).setTextColor(Color.parseColor("#00ff00"));
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }

    public void setNumberPickerMidTextSize(int size) {
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields){
            if(pf.getName().equals("mInputText")){ //
                pf.setAccessible(true);

                //设置分割线的颜色
                try {
                    ((EditText) pf.get(this)).setTextSize(size);
                    ((EditText) pf.get(this)).setTextColor(Color.parseColor("#00ff00"));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void setNumberPickerDividerColor(int color){
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields){
            if(pf.getName().equals("mSelectionDivider")){ //找到mSelectionDivider
                pf.setAccessible(true);

                //设置分割线的颜色
                try {
                    pf.set(this, new ColorDrawable(color));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
